<template>
	<view>
		<uv-navbar leftIcon='' bg-color="rgba(0,0,0,0)">
			<template v-slot:center>
				<view class="nav-search-box" @click="toSearchPage">
					<uni-icons class="nav-search-icon" type="search" size="20"></uni-icons>
					<text class="nav-search-text">搜索</text>
				</view>
			</template>
		</uv-navbar>
		<view :style="topHeightStyle" class="top-height-view"></view>
		<unicloud-db ref="udb" collection="tian-mall-shops" field="ID,shop_name,address,banner,shop_desc"
			:page-size="10" @load="onDataLoad" :model="shopsData"
			v-slot:default="{data, pagination, loading, hasMore, error, options}">
			<uv-list>
				<template v-for="(item,index) in shopsData" :key="item._id">
					<uv-list-item>
						<view class="list-item-box">
							<view class="item-in-box">
								<view class="image-box" v-if="item.banner">
									<image :src="$thumbImg(item.banner,'250x250')" class="item-image" mode="aspectFit"
										@click="previewImage([item.banner])"></image>
								</view>
								<view class="banner-image" v-else></view>

								<view class="right-box" @click="navToShop(item._id,item.ID)">
									<view class="name-box">
										<text>{{item.shop_name.short_name.length>8 ? item.shop_name.short_name.slice(0,8)+'...':item.shop_name.short_name}}</text>
									</view>
									<view class="address-box">
										<wjf-iconfont code="e614" color="deepskyblue" size="15"></wjf-iconfont>
										<text>{{item.address.address_number}}</text>
									</view>
									<view class="desc-box">
										<wjf-iconfont code="e63e" color="deepskyblue" size="15"></wjf-iconfont>
										<text>{{item.shop_desc}}</text>
									</view>
								</view>

							</view>
						</view>
					</uv-list-item>
				</template>
			</uv-list>
			<uv-load-more :status="loadStatus">{{loading}}</uv-load-more>
		</unicloud-db>

	</view>
</template>

<script>
	const db = uniCloud.database()
	export default {
		data() {
			return {
				statusBarHeigh: 0,
				shopsData: [],
				loadStatus: 'loadmore',
			}
		},
		computed: {
			topHeightStyle() {
				return {
					height: uni.getSystemInfoSync().statusBarHeight + 44 + 'px',
					"background-color": "#ffffff"
				}
			}
		},
		onLoad() {
			// this.getShopsData()
		},
		onReachBottom() {
			// console.log('123');
			this.loadStatus = 'loading';
			this.$refs.udb.loadMore()
		},
		methods: {
			async getShopsData() {
				await db.collection('tian-mall-shops').where({
					online: true,
					state: true
				}).orderBy('ID asc').field('ID,shop_name,address,banner,shop_desc').get().then(({
					result
				}) => {
					// console.log(result.data);
					this.shopsData = result.data
				})
			},
			// 加载数据成功的回调
			onDataLoad(e) {
				// console.log('读取数据库数据', e);
				const data = e;
				if (data && data.length) {
					// 将新数据加入到现有的list中
					this.shopsData = [...this.shopsData, ...data];
					this.loadStatus = 'loadmore';
				} else {
					this.loadStatus = 'nomore';
				}
			},
			previewImage(img) {
				uni.previewImage({
					urls: img
				})
			},
			navToShop(id, shopID) {
				uni.navigateTo({
					url: '../shop/shop?id=' + id + '&shopID=' + shopID,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			toSearchPage() {
				uni.navigateTo({
					url: '/pages/utils/search-shop',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.nav-search-box {
		width: 25vw;
		/* #ifdef MP-WEIXIN */
		margin-left: -5vw;
		/* #endif */
		background-color: rgba(230, 230, 230, 0.8);
		display: flex;
		// justify-content: space-between;
		align-items: center;
		border-radius: 30rpx;
		padding: 10rpx 30rpx;

		.nav-search-icon {
			margin-right: 40rpx;
		}

		.nav-search-text {
			color: #555;
			font-size: 32rpx;
		}
	}

	.top-height-view {
		margin-bottom: 40rpx;
	}

	.uv-list-box {
		margin: 20rpx 30rpx;
		margin: 0 30rpx;
		background-color: #555;
	}

	.list-item-box {
		// margin: 10rpx 0;
		width: 100vw;
		padding: 8rpx 0rpx;
		background-color: #eee;
		display: flex;
		align-items: center;

		.item-in-box {
			width: 96%;
			margin: 0 30rpx;
			border-radius: 10rpx;
			background-color: #fff;
			display: flex;
			align-items: center;

			.image-box {
				width: 150rpx;
				height: 150rpx;
				// margin: 10rpx 0 10rpx 20rpx;
				margin-left: 20rpx;

				.item-image {
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
			}

			.banner-image {
				width: 150rpx;
				height: 150rpx;
				margin-left: 20rpx;
				border-radius: 10rpx;
				background-image: url('https://wjf-5866.oss-cn-hongkong.aliyuncs.com/henghui/static/shop-banner.png');
				background-size: cover;
				/* 背景图片覆盖整个元素 */
				background-position: center;
				/* 背景图片居中 */
			}


			.right-box {
				flex: 1;
				margin: 20rpx 0 20rpx 20rpx;
				padding: 20rpx 0;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.name-box {
					text {
						font-weight: 600;
						font-size: 30rpx;
						color: #555;
					}
				}

				.address-box {
					display: flex;
					align-items: center;

					text {
						margin-left: 10rpx;
						font-size: 28rpx;
						color: #777;
					}
				}

				.desc-box {
					display: flex;
					align-items: center;

					text {
						margin-left: 10rpx;
						font-size: 28rpx;
						color: #777;
						display: flexbox;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
					}
				}
			}
		}
	}
</style>